/**
* AnagramDetector.java 1.0 Jan 14, 2014
*
* An anagram is a word or a phrase that can be created by rearranging the 
* letters of another given word or phrase. We ignore white spaces and letter 
* case. The all letters of "Desperation" can be rearranged to the phrase "A Rope
* Ends It". The program should receive input of two strings and give the output
* to screen telling whether these words are anagrams
*/

import java.util.*;

public class AnagramDetector{
	
	public static void main(String[] args){
		
		Scanner in = new Scanner(System.in);
		System.out.println("Enter first string:");
		String s1 = in.nextLine();
		System.out.println("Enter second string:");
		String s2 = in.nextLine();
		
		s1 = s1.toLowerCase();
		s2 = s2.toLowerCase();
		
		//Delete spaces in the string
		for (int i = 0; i < s1.length(); i++){
		
			//Decrement i for case when two spaces in a row are present
			if (s1.charAt(i) == ' ') s1 = deleteFromString(s1, i--);
		}
		for (int i = 0; i < s2.length(); i++){
		
			//Decrement i for case when two spaces in a row are present
			if (s2.charAt(i) == ' ') s2 = deleteFromString(s2, i--);
		}
		
		for (int i = 0; i < s1.length(); i++){
			for (int j = 0; j < s2.length(); j++){
				if (s1.charAt(i) == (s2.charAt(j))){
					s1 = deleteFromString(s1, i);
					
					/*Need to check s1.charAt(i) once again because there will be new
					character there*/
					i--; 
					s2 = deleteFromString(s2, j);
					break;
				}
			}
		}
		if ((s1.length() == 0) && (s2.length() == 0))	
			System.out.println("Anagram detected");	
		else
			System.out.println("Anagram not detected");	
	}
	private static String deleteFromString(String s, int n){
		return s.substring(0, n) + s.substring(n + 1);
	}
}